package arr;

/**
 * @author : Rookie
 * @description : 大于给定值的最短子数组长度
 * @date : 2023/8/18
 */
public class ShortestSubSequence {

    public static void main(String[] args) {
        int target = 7;
        int[] nums = new int[]{2,3,1,2,4,3};
        System.out.println(doublePointSolute(target,nums));
//        System.out.println(violentSolute(target,nums));
    }

    private static int violentSolute(int target,int[]nums) {
        int result = Integer.MAX_VALUE;
        int subLength = 0;
        int sum = 0;
        for (int i = 0; i < nums.length; i++) {
            sum = 0;
            for (int j = i; j < nums.length; j++) {
                sum+=nums[j];
                if (sum >= target) {
                    subLength = j -i+1;
                    result = Math.min(result, subLength);
                    break;
                }
            }
        }
        return result == Integer.MAX_VALUE ? 0:result;
    }

    private static int doublePointSolute(int target,int []nums) {
        int result = Integer.MAX_VALUE;
        int subLength = 0;
        int sum = 0,slow = 0;

        for (int fast = 0; fast < nums.length; fast++) {
            sum +=nums[fast];
            while (sum >= target) {
                subLength = fast -slow+1;
                result = Math.min(result,subLength);
                sum-=nums[slow];
                // 移动起始位置
                slow++;
            }
        }
        return result == Integer.MAX_VALUE ? 0:result;
    }
}
